package adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qianfeng.lingshijia.R;

import java.util.ArrayList;
import java.util.List;

import entity.SchoolMarketEntity;
import utils.DownloadUtils;

public class SchoolMarketSingleAdapter extends BaseAdapter {

    private List<SchoolMarketEntity.DataEntity> list;

    private Context context;
    private LayoutInflater layoutInflater;

    public SchoolMarketSingleAdapter(Context context, List<SchoolMarketEntity.DataEntity> list) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public SchoolMarketEntity.DataEntity getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.school_market_single_layout, null);
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.ivShoppingSingleImg = (ImageView) convertView.findViewById(R.id.ivShoppingSingleImg);
            viewHolder.tvMarketName = (TextView) convertView.findViewById(R.id.tvMarketName);
            viewHolder.tvMarketState = (TextView) convertView.findViewById(R.id.tvMarketState);
            viewHolder.ivStart1 = (ImageView) convertView.findViewById(R.id.ivStart_1);
            viewHolder.ivStart2 = (ImageView) convertView.findViewById(R.id.ivStart_2);
            viewHolder.ivStart3 = (ImageView) convertView.findViewById(R.id.ivStart_3);
            viewHolder.ivStart4 = (ImageView) convertView.findViewById(R.id.ivStart_4);
            viewHolder.ivStart5 = (ImageView) convertView.findViewById(R.id.ivStart_5);
            viewHolder.tvSellGoodsNumber = (TextView) convertView.findViewById(R.id.tvSellGoodsNumber);
            viewHolder.lMarketEvaluate = (LinearLayout) convertView.findViewById(R.id.lMarketEvaluate);
            viewHolder.lMarketFeature = (LinearLayout) convertView.findViewById(R.id.lMarketFeature);
            viewHolder.tvMarketFeature = (TextView) convertView.findViewById(R.id.tvMarketFeature);
            viewHolder.tvSchoolMarketAddress = (TextView) convertView.findViewById(R.id.tvSchoolMarketAddress);
            viewHolder.tvMarketReduce = (TextView) convertView.findViewById(R.id.tvMarketReduce);
            viewHolder.lMarkReduce = (LinearLayout) convertView.findViewById(R.id.lMarketReduce);

            convertView.setTag(viewHolder);
        }
        //数据加载
        initializeViews(getItem(position), (ViewHolder) convertView.getTag());
        return convertView;
    }

    public boolean setStart(ViewHolder holder, int startCount) {
        List<ImageView> list = new ArrayList<ImageView>();
        list.add(holder.ivStart1);
        list.add(holder.ivStart2);
        list.add(holder.ivStart3);
        list.add(holder.ivStart4);
        list.add(holder.ivStart5);
        if (0 <= startCount && startCount <= 35) {
            int oneStartNo = startCount / 7;
            int leftStartCount = startCount % 7;
            int halfStartCount = 0;
            if (leftStartCount > 3) {
                halfStartCount = 1;
            }
            for (int i = 0; i < 5; i++, oneStartNo--) {
                if (oneStartNo > 0) {
                    list.get(i).setImageResource(R.drawable.star_level_3_img);
                } else if (halfStartCount == 1) {
                    halfStartCount--;
                    list.get(i).setImageResource(R.drawable.star_level_2_img);
                } else {
                    list.get(i).setImageResource(R.drawable.star_level_1_img);
                }
            }
            return true;
        } else {
            return false;
        }
    }

    //加载数据
    public void initializeViews(SchoolMarketEntity.DataEntity dEntity, ViewHolder holder) {
        holder.getlMarketFeature().setVisibility(View.GONE);//设置特点栏不可见
        holder.getlMarkReduce().setVisibility(View.GONE);//设置减价栏不可见
        holder.getlMarketEvaluate().removeAllViews();//清理之前的评价
        holder.getIvShoppingSingleImg().setImageBitmap(null);//设置店铺图片为空

        DownloadUtils.LoadPicture(dEntity.getHeadImg(), holder.getIvShoppingSingleImg());//加载图片
        holder.getTvMarketName().setText(dEntity.getShopName());//设置甜皮名称
        holder.getTvMarketState().setText(dEntity.getDeliverStatus());//设置店铺状态
        setStart(holder, dEntity.getStarCount());//设置店铺星级
        holder.getTvSellGoodsNumber().setText("月销" + dEntity.getCount() + "份");//设置销量
        holder.getTvSchoolMarketAddress().setText("(" + dEntity.getAddress() + ")");//设置地址
        //设置减价
        if (dEntity.getManjians() != null && dEntity.getManjians().size() != 0) {
            holder.getlMarkReduce().setVisibility(View.VISIBLE);
            holder.getTvMarketReduce().setText(dEntity.getManjians().get(0).getTitle());
        }
        //设置特点
        if (dEntity.getCombinationList() != null && dEntity.getCombinationList().size() != 0) {
            holder.getlMarketFeature().setVisibility(View.VISIBLE);
            holder.getTvMarketFeature().setText(dEntity.getCombinationList().get(0).getCombinSpec());
        }
        //设置评价
        if (dEntity.getImpressList() != null) {
            for (int i = 0; i < dEntity.getImpressList().size(); i++) {
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
                        (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.setMargins(5, 5, 5, 5);
                TextView tv = new TextView(context);
                tv.setLayoutParams(params);
                tv.setBackgroundResource(R.drawable.tag_background);
                tv.setTextSize(13);
                tv.setPadding(20, 7, 20, 7);
                tv.setText(dEntity.getImpressList().get(i).getImpress());
                holder.getlMarketEvaluate().addView(tv);
            }
        }
    }

    protected class ViewHolder {
        private ImageView ivShoppingSingleImg;
        private TextView tvMarketName;
        private TextView tvMarketState;
        private ImageView ivStart1;
        private ImageView ivStart2;
        private ImageView ivStart3;
        private ImageView ivStart4;
        private ImageView ivStart5;
        private TextView tvSellGoodsNumber;
        private LinearLayout lMarketEvaluate;
        private LinearLayout lMarketFeature;
        private TextView tvMarketFeature;
        private TextView tvSchoolMarketAddress;
        private TextView tvMarketReduce;
        private LinearLayout lMarkReduce;

        public ImageView getIvShoppingSingleImg() {
            return ivShoppingSingleImg;
        }

        public TextView getTvMarketName() {
            return tvMarketName;
        }

        public TextView getTvMarketState() {
            return tvMarketState;
        }

        public TextView getTvSellGoodsNumber() {
            return tvSellGoodsNumber;
        }

        public LinearLayout getlMarketEvaluate() {
            return lMarketEvaluate;
        }

        public LinearLayout getlMarketFeature() {
            return lMarketFeature;
        }

        public TextView getTvMarketFeature() {
            return tvMarketFeature;
        }

        public TextView getTvSchoolMarketAddress() {
            return tvSchoolMarketAddress;
        }

        public TextView getTvMarketReduce() {
            return tvMarketReduce;
        }

        public LinearLayout getlMarkReduce() {
            return lMarkReduce;
        }
    }
}
